import { request } from './index'
import type { DailyReport, WorkTimeStats } from '@/types'

export const reportApi = {
  // 获取所有日报
  getAll(): Promise<DailyReport[]> {
    return request.get('/daily-reports')
  },

  // 根据ID获取日报
  getById(id: number): Promise<DailyReport> {
    return request.get(`/daily-reports/${id}`)
  },

  // 创建日报
  create(data: Omit<DailyReport, 'id' | 'createdAt' | 'updatedAt'>): Promise<DailyReport> {
    return request.post('/daily-reports', data)
  },

  // 更新日报
  update(id: number, data: Partial<DailyReport>): Promise<DailyReport> {
    return request.put(`/daily-reports/${id}`, data)
  },

  // 删除日报
  delete(id: number): Promise<void> {
    return request.delete(`/daily-reports/${id}`)
  },

  // 获取我的日报
  getMy(): Promise<DailyReport[]> {
    return request.get('/daily-reports/my-reports')
  },

  // 根据项目ID获取日报
  getByProject(projectId: number): Promise<DailyReport[]> {
    return request.get(`/daily-reports/project/${projectId}`)
  },

  // 根据用户ID获取日报
  getByUser(userId: number): Promise<DailyReport[]> {
    return request.get(`/daily-reports/user/${userId}`)
  },

  // 获取指定用户指定日期的日报
  getByUserAndDate(userId: number, date: string): Promise<DailyReport> {
    return request.get(`/daily-reports/user/${userId}/date/${date}`)
  },

  // 获取今日日报
  getToday(): Promise<DailyReport> {
    return request.get('/daily-reports/my-today')
  },

  // 根据日期范围获取日报
  getByDateRange(startDate: string, endDate: string): Promise<DailyReport[]> {
    return request.get(`/daily-reports/date-range?startDate=${startDate}&endDate=${endDate}`)
  },

  // 根据用户ID和日期范围获取日报
  getByUserAndDateRange(userId: number, startDate: string, endDate: string): Promise<DailyReport[]> {
    return request.get(`/daily-reports/user/${userId}/date-range?startDate=${startDate}&endDate=${endDate}`)
  },

  // 检查今日是否已提交日报
  checkTodayReported(): Promise<{ reported: boolean }> {
    return request.get('/daily-reports/check-today')
  },

  // 获取工作时长统计
  getWorkTimeStats(userId?: number): Promise<WorkTimeStats> {
    const url = userId ? `/daily-reports/work-time-stats/${userId}` : '/daily-reports/work-time-stats'
    return request.get(url)
  },

  // 获取我的工作时长统计
  getMyWorkTimeStats(): Promise<WorkTimeStats> {
    return request.get('/daily-reports/my-work-time-stats')
  },

  // 搜索日报
  search(keyword: string): Promise<DailyReport[]> {
    return request.get(`/daily-reports/search?keyword=${keyword}`)
  },

  // 导出日报
  export(params: {
    startDate?: string
    endDate?: string
    userId?: number
    projectId?: number
    format?: 'excel' | 'pdf'
  }): Promise<Blob> {
    return request.get('/daily-reports/export', {
      params,
      responseType: 'blob'
    })
  },

  // 获取日报统计
  getStats(params: {
    startDate?: string
    endDate?: string
    userId?: number
    projectId?: number
  }): Promise<any> {
    return request.get('/daily-reports/stats', { params })
  }
}